<%@page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="com.googlecode.contests_repo.core.users.AccessRight"%>
<%@page import="com.googlecode.contests_repo.core.users.UserManager"%>
<%@page import="com.googlecode.contests_repo.core.model.*"%>
<%@page import="com.googlecode.contests_repo.servlets.*"%>
<%@page import="com.googlecode.contests_repo.core.WebTools"%>
<%@page import="java.io.File"%>
<%@page import="java.util.*"%>

	<%!private String getMessage(Map<String, String> errorMap, String key) {
		if (errorMap == null) {
			return "";
		}

		String message = errorMap.get(key);
		return message == null ? "" : message;
	}%>
	<%
		if (request.getParameter("submittedContes") != null) {
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/ContestEditServlet");
			dispatcher.include(request, response);
		}

		String contestId = request.getParameter("path");
		String repoPath = ConfigurationManager.getInstance().getRepoRoot();
		String contestPath = repoPath + File.separator + contestId;
		Contest contest = null;
		Contest fileSystemContest = ResourcesUtils.getJSON(new File(
				contestPath));
		if (request.getAttribute("savedContest") != null) {
			contest = (Contest) request.getAttribute("savedContest");
		} else {
			contest = fileSystemContest;
		}
		if (fileSystemContest == null) {
	%>
	<p>The requested contest does not exist.</p>
	<%
		} else {
			String loggedUser = (String) session.getAttribute("username");

			if (!UserManager.hasAccessRight(loggedUser, contestPath,
					AccessRight.VIEW)) {
	%>
	<p>You don't have permission to view this contest.</p>
	<%
		} else {
				// get edited diration
				String duration = (String) request
						.getAttribute(ContestEditServlet.INVALID_DURATION_KEY);
				if (duration == null) {
					duration = "" + contest.getDuration();
				}
				// get resources names
				String publicFolder = "_public_files";
				String privateFolder = "_files";
				// check edit rights
				boolean canEdit = "1".equals(request.getParameter("edit"));
				boolean hasUserEditRights = UserManager.hasAccessRight(
						loggedUser, contestPath, AccessRight.VIEW_FULL);
				String readOnly = (!canEdit || !hasUserEditRights) ? "readonly=\"readonly\""
						: "";
				// get edit error map
				Map<String, String> errorMap = (Map<String, String>) request
						.getAttribute("errorMap");
	%>

	<h1>Contest Info</h1>

	<form method="post" action="">
		<p>
			<b>Id: </b> <input type="text" name="id"
				readonly="readonly" value="<%=contest.getId()%>" />
		</p>
		<p>
			<b>Title: </b> <input type="text"
				name="title" <%=readOnly%> value="<%=contest.getTitle()%>" />
			<input type="hidden" name="old-title"
				value="<%=fileSystemContest.getTitle()%>" />
		</p>
		<p>
			<b>About: </b> <input type="text"
				name="about" <%=readOnly%> value="<%=contest.getAbout()%>" />
			<input type="hidden" name="old-about"
				value="<%=fileSystemContest.getAbout()%>" />
		</p>
		<p>
			<b>Start time: </b> <input type="text"
				name="start-time" <%=readOnly%> value="<%=contest.getStartTime()%>" />
			<span class="error"> <%=getMessage(errorMap,
							ContestEditServlet.START_TIME_KEY)%> </span> <input type="hidden"
				name="old-start-time" value="<%=fileSystemContest.getStartTime()%>" />
		</p>
		<p>
			<b>Duration: </b> <input type="text"
				name="duration" <%=readOnly%> value="<%=duration%>" /> <span
				class="error"> <%=getMessage(errorMap,
							ContestEditServlet.DURATION_KEY)%> </span> <input type="hidden"
				name="old-duration" value="<%=fileSystemContest.getDuration()%>" />
		</p>
		<p>
			<b>Grading style: </b> <%
 	String gradingStyle = contest.getGradingStyle();
 			String checkedACM = gradingStyle.equals("acm") ? "checked=\"checked\""
 					: "";
 			String checkedIOI = gradingStyle.equals("ioi") ? "checked=\"checked\""
 					: "";
 			if ("".equals(readOnly)) {
 %> <input type="radio" name="grading-style" <%=checkedACM%> value="acm" />
				acm <input type="radio" name="grading-style" <%=checkedIOI%>
				value="ioi" /> ioi <input type="hidden"
				name="old-grading-style" value="<%=gradingStyle%>" /> <%
 	} else {
 %> <%=gradingStyle%> <%
 	}
 %>
		</p>
		<%
			if (hasUserEditRights
							&& ResourcesUtils
									.hasStarted(contest.getStartTime())) {
						String problemOrder = WebTools.arrayToString(contest
								.getProblemOrder());
						String oldProblemOrder = WebTools
								.arrayToString(fileSystemContest
										.getProblemOrder());
						String problemScores = (String) request
								.getAttribute(ContestEditServlet.INVALID_SCORES_KEY);
						String oldProblemScores = WebTools
								.arrayToString(fileSystemContest
										.getProblemScores());
						if (problemScores == null) {
							problemScores = WebTools.arrayToString(contest
									.getProblemScores());
						}
		%>
		<p>
			<b>Problem order: </b> <input type="text"
				name="problem-order" <%=readOnly%> value="<%=problemOrder%>" />
				<span class="error"> <%=getMessage(errorMap,
								ContestEditServlet.PROBLEM_ORDER_KEY)%></span> <input type="hidden"
				name="old-problem-order" <%=readOnly%> value="<%=oldProblemOrder%>" />
		</p>
		<p>
			<b>Problem scores: </b> <input type="text"
				name="problem-scores" <%=readOnly%> value="<%=problemScores%>" />
				<span class="error"> <%=getMessage(errorMap,
								ContestEditServlet.PROBLEM_SCORES_KEY)%></span> <input type="hidden"
				name="old-problem-scores" <%=readOnly%>
				value="<%=oldProblemScores%>" />
		</p>
		<%
			}
					if (hasUserEditRights) {
		%>
		<p>
			<b>Notes: </b> <input type="text"
				name="notes" <%=readOnly%> value="<%=contest.getNotes()%>" />
			<input type="hidden" name="old-notes"
				value="<%=fileSystemContest.getNotes()%>" />
		</p>
		<%
			if (canEdit) {
		%>
		<p><input type="submit" name="submittedContes" value="Change" />
		<a href="resource.jsp?path=<%=contestId%>">Discard changes</a>
		</p>
		<%
			}
					}
		%>
	</form>
	<h2>Problems:</h2>
	<table>
		<tr>
			<th>Id</th>
			<th>Title</th>
			<th>About</th>
			<th>View</th>
			<th>Download</th>
			<%
				if (canEdit) {
			%>
			<th>Edit</th>
			<%
				}
						if (canEdit && hasUserEditRights) {
			%>
			<th>Delete</th>
			<%
				}
			%>
		</tr>
		<%
			List<Problem> problemList = ResourcesUtils
							.fetchResources(new File(contestPath));
					if (problemList != null) {
						for (Problem problem : problemList) {
							String problemPath = contestPath + File.separator
									+ problem.getId();
							if (UserManager.hasAccessRight(loggedUser,
									problemPath, AccessRight.VIEW_FULL)
									|| ResourcesUtils.hasStarted(contest
											.getStartTime())) {
		%>
		<tr>
			<td><%=problem.getId()%></td>
			<td><%=problem.getTitle()%></td>
			<%
				if (UserManager.hasAccessRight(loggedUser,
											problemPath, AccessRight.VIEW)) {
			%>
			<td><%=problem.getAbout()%></td>
			<td><a
				href="resource.jsp?path=<%=contestId + File.separator
											+ problem.getId()%>">details</a>
			</td>
			<td><a
				href="fetchZipped?problem=<%=problem.getId()%>&amp;contest=<%=contestId%>">download</a>
			</td>
			<%
				}
									if (canEdit
											&& UserManager.hasAccessRight(
													loggedUser, problemPath,
													AccessRight.VIEW_FULL)) {
			%>
			<td><a
				href="resource.jsp?path=<%=String.format("%s%s%s",
											contestId, File.separator,
											problem.getId())%>&amp;edit=1">edit</a>
			</td>
			<%
				}
									if (canEdit && hasUserEditRights) {
			%>
			<td><a
				href="delete_file?path=<%=String.format("%s%s%s",
											contestId, File.separator,
											problem.getId())%>">delete</a>
			</td>
			<%
				}
								}
			%>
		</tr>
		<%
			}

					}
		%>

	</table>
	<%
		}
		}
	%>
